PHP - Conditional statements
Conditional statements are used whenever we work on some conditions to execute a particular set of instructions if those conditions are set to be true. These statements control the flow of execution within the program based on the desired conditions. There are three decision-making statements that are supported by PHP.
- If statement
- If-else statement
- If- Elseif-else statement
- Switch statement
1. If Statement
This statement will check for the given condition. If the condition gives a true result, the following set of instructions will get executed.
Syntax
Example
Output
2. If- else statement
With this expression, you will get to test a given condition if the result of the condition is true then the following set of statements will get executed. In case if you want to perform some action even if the condition is false then the statements will be included within the else block.
Syntax
Example
Output
3. If-elseif-else statement
In this expression, you will get to check more than one condition to execute some instruction if any of the conditions proves to be true. If none of the conditions comes out to be true then the else block which is mostly defined at the end of the program, will get executed.
Syntax
Example
Output
4. Switch statement
Using this statement, you can avoid the long if-elseif-else block of code. With this statement, you are able to execute many blocks at a time having a different condition to be checked.
Syntax
Example
Output
People are also reading: